package com.hmall.item.web;

import com.hmall.item.pojo.Item;
import com.search.common.dto.PageDTO;


import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO getById(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        return itemService.pageGet(page, size);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable long id){
        return itemService.getById(id);
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void modifyTheState(@PathVariable(value = "id") long id,@PathVariable(value = "status") Integer status){
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        item.setUpdateTime(new Date());
        itemService.updateById(item);
    }

    /**
     * 修改商品信息
     * @param item
     */
    @PutMapping
    public void upById(@RequestBody Item item){
        item.setUpdateTime(new Date());
        itemService.updateById(item);
    }

    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("{id}")
    public void del(@PathVariable long id){
        itemService.removeById(id);
    }
    @GetMapping("/get")
    public List<Item> getItems(){
        return itemService.getAll();
    }

    /**
     * 扣减库存
     * @param itemId
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void UpStock(@PathVariable("itemId") long itemId,@PathVariable("num") Integer num){
        Item item = itemService.getById(itemId);
        item.setStatus(item.getStatus()-num);
        itemService.updateById(item);
    }

}
